package com.cskaoyan.service.wxorder;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.po.ohter.MarketGoods;
import com.cskaoyan.bean.po.wxorder.MarketFootprint;
import com.cskaoyan.bean.po.wxorder.MarketFootprintExample;
import com.cskaoyan.bean.vo.wxorder.WxFootprintVo;
import com.cskaoyan.mapper.wxorder.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.util.common.BeanTransfer;
import com.cskaoyan.util.common.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-19 21:22
 */
@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;

    @Override
    public CommonData queryFootprint(BasePageInfo basePageInfo,Integer id) {

        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
       criteria.andDeletedEqualTo(false).andUserIdEqualTo(id);
        example.setOrderByClause("add_time desc");
        List<WxFootprintVo> footprintVos = new ArrayList<>();

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());//1,10
        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(example);

        WxFootprintVo wxFootprintVo1 =new WxFootprintVo();

        MarketGoods marketGoods1=null;
        for (MarketFootprint footprint : marketFootprints) {
            WxFootprintVo wxFootprintVo = new WxFootprintVo();
            marketGoods1 = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            try {
                wxFootprintVo1 = (WxFootprintVo) BeanTransfer.transfer(marketGoods1, wxFootprintVo);
                wxFootprintVo1.setId(footprint.getId());
                wxFootprintVo1.setGoodsId(footprint.getGoodsId());
                footprintVos.add(wxFootprintVo1);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        PageInfo<MarketFootprint> info=new PageInfo<>(marketFootprints);
        PageInfo<WxFootprintVo> pageInfo = new PageInfo<>(footprintVos);
        pageInfo.setPages(info.getPages());
        pageInfo.setTotal(info.getTotal());
        // CommonData commonData = new CommonData();
        // commonData.setLimit(info.getPageSize());
        // commonData.setList(footprintVos);
        // commonData.setPage(info.getPageNum());
        // commonData.setPages(info.getPages());
        // commonData.setTotal((int) info.getTotal());
        // return commonData;
        return CommonData.data(pageInfo);

    }
    @Transactional
    @Override
    public void deleteFootprint(Integer id) {
        MarketFootprint marketFootprint = footprintMapper.selectByPrimaryKey(id);
        marketFootprint.setDeleted(true);
        marketFootprint.setUpdateTime(DateUtils.time());
        footprintMapper.updateByPrimaryKeySelective(marketFootprint);


    }
}
